//从node_modules中导依赖
import Vue from 'vue'
import VueRouter from 'vue-router'
//安装路由
Vue.use(VueRouter)

const routes = [
  {
    path: '/',//路由路径
    name: 'Home',
    // 当输入根节点时@/views/Home会加载到App的<router-view/>
    component: () => import('@/views/Home')//跳转的组件
  },
  {
    path: '/register',
    name: 'register',
    // 当输入register时访问@/views/auth/Register页面
    component: () => import('@/views/auth/Register'),
    meta: {title: '注册'}
  },
  // 登录
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/auth/Login'),
    meta: { title: '登录' }
  },
   // 发布帖子
   {
    path: '/post/create',
    name: 'post-create',
    component: () => import('@/views/homeRight/Create'),
    meta: { title: '信息发布', requireAuth: true }
  },
  // 帖子详情
  {
    path: "/post/:id",
    name: "post-detail",
    component: () => import("@/views/homeLeft/Detail"),
  },
  {
    path: '/404',
    name: '404',
    // 当输入404时访问@/views/error/404页面
    component: () => import('@/views/error/404'),
    //打开每一个页面的title
    meta: {title: '404-Notfound'}
  },
  {
    path: '*',
    name: '用户输入上面path之外的路径则返回404',
    // 当输入404时重定向到@/views/error/404页面
    redirect: '/404',
    hidden: true
  }
]
//注销登录后首页还退回到首页异常进行捕捉
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes
})
//配置导出路由
export default router
